name: Build and Push Docker image

on:
  push:
    branches:
      - main
    tags:
      - v*

jobs:
  release_tagged_image:
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - name: Check out the code
        uses: actions/checkout@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Docker Hub Container Registry
        uses: docker/login-action@v3
        with:
          username: marcusschiesser
          password: ${{ secrets.DOCKER_TOKEN }}

      - name: Extract tag name
        id: tag
        run: echo ::set-output name=TAG::${GITHUB_REF#refs/tags/}

      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Dockerfile
          push: true
          platforms: linux/amd64,linux/arm64/v8
          tags: |
            ragapp/ragapp:${{ steps.tag.outputs.TAG }}
            ragapp/ragapp:latest

  release_dev_image:
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - name: Check out the code
        uses: actions/checkout@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Dockerfile
          push: true
          platforms: linux/amd64,linux/arm64/v8
          tags: ghcr.io/${{ github.repository }}:latest
